package com.sg.service.base.calc.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.calc.req.QueryThemeEvaluationCrossDetailReq;
import com.sg.dto.base.calc.req.QueryThemeEvaluationCrossListReq;
import com.sg.entity.OmsThemeEvaTemp;
import com.sg.service.base.calc.MOmsThemeEvaTempService;
import com.wicket.okrcalc.common.dto.FieldInfo;
import com.wicket.okrcalc.common.exception.BizException;
import com.wicket.okrcalc.dal.mapper.mbg.OmsThemeEvaTempMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsThemeEvaTempServiceImpl
        extends ServiceImpl<OmsThemeEvaTempMapper, OmsThemeEvaTemp>
        implements MOmsThemeEvaTempService {

    @Autowired
    OmsThemeEvaTempMapper omsThemeEvaTempMapper;

    /**
     * 4-2-02新增主题跨评价模板范围[10119]
     * gen by moon at 8/21/2024, 12:19:20 PM
     */
    @Trace(operationName = "4-2-02新增主题跨评价模板范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addThemeEvaluationCross(OmsThemeEvaTemp omsThemeEvaTemp) {
        omsThemeEvaTemp.setThemeEvaTempId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsThemeEvaTemp.setIsArchive("FALSE");
        omsThemeEvaTemp.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsThemeEvaTemp.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsThemeEvaTemp != null && omsThemeEvaTemp.getSubjectLifeCycle() != null && omsThemeEvaTemp.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsThemeEvaTemp.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsThemeEvaTemp != null && omsThemeEvaTemp.getSubjectLifeCycle() != null && omsThemeEvaTemp.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsThemeEvaTemp.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsThemeEvaTemp.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsThemeEvaTemp.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsThemeEvaTemp != null && omsThemeEvaTemp.getSubjectLifeCycle() != null && omsThemeEvaTemp.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsThemeEvaTemp.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsThemeEvaTemp != null && omsThemeEvaTemp.getSubjectLifeCycle() != null && omsThemeEvaTemp.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsThemeEvaTemp.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        omsThemeEvaTemp.setIsValid("FALSE");
        omsThemeEvaTemp.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsThemeEvaTemp.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsThemeEvaTemp)) {
            return omsThemeEvaTemp.getThemeEvaTempId();
        } else {
            return "-1";
        }

    }

    /**
     * 4-2-02查询主题跨评价模板范围列表[10121]
     * gen by moon at 8/21/2024, 12:19:23 PM
     */
    @Trace(operationName = "4-2-02查询主题跨评价模板范围列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsThemeEvaTemp> queryThemeEvaluationCrossList(QueryThemeEvaluationCrossListReq reqDto) {

        try {
            QueryWrapper<OmsThemeEvaTemp> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsThemeEvaTemp.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsThemeEvaTemp.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsThemeEvaTemp.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsThemeEvaTemp>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 4-2-02修改主题跨评价模板范围[10129]
     * gen by moon at 8/21/2024, 12:19:24 PM
     */
    @Trace(operationName = "4-2-02修改主题跨评价模板范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateThemeEvaluationCross(OmsThemeEvaTemp omsThemeEvaTemp) {
        omsThemeEvaTemp.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsThemeEvaTemp != null && omsThemeEvaTemp.getSubjectLifeCycle() != null && omsThemeEvaTemp.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsThemeEvaTemp.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsThemeEvaTemp != null && omsThemeEvaTemp.getSubjectLifeCycle() != null && omsThemeEvaTemp.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsThemeEvaTemp.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsThemeEvaTemp.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsThemeEvaTemp != null && omsThemeEvaTemp.getSubjectLifeCycle() != null && omsThemeEvaTemp.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsThemeEvaTemp.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsThemeEvaTemp != null && omsThemeEvaTemp.getSubjectLifeCycle() != null && omsThemeEvaTemp.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsThemeEvaTemp.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsThemeEvaTemp);
    }

    /**
     * 4-2-02查询主题跨评价模板范围详情[10171]
     * gen by moon at 8/24/2024, 11:00:33 AM
     */
    @Trace(operationName = "4-2-02查询主题跨评价模板范围详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsThemeEvaTemp queryThemeEvaluationCrossDetail(QueryThemeEvaluationCrossDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsThemeEvaTemp> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsThemeEvaTemp> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsThemeEvaTemp retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
